<?php
namespace App\Service\Member;

class DoctorService extends BaseService
{
    //医生


    //选项
    public function getOption()
    {
        //科室+医院+咨询类型
        $arys = ['keshi' => [], 'yiyuan' => [], 'seek' => []];
        //科室选项
        $departs = $this->dbConn($this->dbCate)
            ->where('del', 0)
            ->where('type', 3)
            ->get();
        if (count($departs)) {
            foreach ($departs as $v) {
                $arys['keshi'][] = [
                    'id' => $v->id,
                    'title' => $v->title,
                ];
            }
        }
        //医院选项
        $yys = $this->dbConn($this->dbYiyuan)
            ->where('del', 0)
            ->get();
        if (count($yys)) {
            foreach ($yys as $v) {
                $arys['yiyuan'][] = [
                    'id' => $v->id,
                    'title' => $v->title,
                ];
            }
        }
        //咨询类型
        $seeks = $this->dbConn($this->dbCate)
            ->where('del', 0)
            ->where('type', 4)
            ->get();
        if (count($seeks)) {
            foreach ($seeks as $v) {
                $v = (array) $v;
                $icon = file_exists($v['icon']) ? env('DOMAIN').'/'.$v['icon'] : '';
                $arys['seek'][] = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                    'icon' => $icon,
                ];
            }
        }
        return $arys;
    }

    /**
     * 列表
     * @param array $pages【start，limit】
     * @param array $pas
     */
    public function getList($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDoctor)->where('del', 0);
        if ($pas['dname']) {
            $uOnes = $this->dbConn($this->dbUser)
                ->where('off', 0)
                ->where('uname', 'like', '%'.$pas['dname'].'%')
                ->get();
            if (count($uOnes)) {
                $uids = [];
                foreach ($uOnes as $v) {
                    $uids[] = $v->id;
                }
                $db = $db->whereIn('uid', $uids);
            }
        }
        if (isset($pas['seek_id']) && $pas['seek_id']) {
            $db = $db->where('seek_type', $pas['seek_id']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //用户信息
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['uid'])
                ->first();
            $uname = !empty($uOne) ? $uOne['realname'] : '-';
            $uhead = '';
            if ($uOne && file_exists($uOne['head'])) {
                $uhead = $uOne['head'];
            }
            //擅长
            $skillList = $v['skill'] ? explode('||', $v['skill']) : [];
            $skillStr = $skillList ? implode('，', $skillList) : '-';
            //咨询价目配置
//            $price = 0;
//            if (isset($pas['seek_id']) && $pas['seek_id']) {
//                $seekOne = (array) $this->dbConn($this->dbUdSeek)
//                    ->where('del', 0)
//                    ->where('doctor_id', $v['id'])
//                    ->where('seek_id', $pas['seek_id'])
//                    ->first();
//                if (!empty($seekOne)) {
//                    $price = toNumber($seekOne['price']) . ' / ' . $seekOne['unit'];
//                }
//            }
            $price = toNumber($v['seek_price']);
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'uname' => $uname,
                'uhead' => $uhead,
                'work_type_name' => $this->cateNameById($v['work_type']),
                'work_title_name' => $this->cateNameById($v['work_title']),
                'hospital_name' => $this->hospitalNameById($v['hospital']),
                'depart_name' => $this->cateNameById($v['hospital']),
                'skillstr' => $skillStr,
                'seek_num' => $v['seek_num'], //已帮助患者数量
                'seek_price' => $price, //价格元
                'seek_type' => $v['seek_type'], //咨询类型
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //医护申请
    public function apply($pas)
    {
        $res = (array) $this->dbConn($this->dbDoctor)
            ->where('del', 0)
            ->where('uid', $pas['uid'])
            ->first();
        if (!empty($res) && in_array($res['is_apply'], [1, 3])) {
            if ($res['is_apply'] == 1) {
                return $this->toback(-2, '已在申请中');
            }
            if ($res['is_apply'] == 2) {
                return $this->toback(-2, '已被拒绝');
            }
            if ($res['is_apply'] == 3) {
                return $this->toback(-2, '已通过');
            }
        }
        $pas['addtime'] = $pas['uptime'] = toDate();
        $lastid = $this->dbConn($this->dbDoctor)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 代办列表
     * @param int $uid 医生UID
     * @param array $pages【start，limit】
     */
    public function daibanList($uid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDaiban)
            ->where('del', 0)
            ->where('did', $uid);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['uid'])
                ->first();
            $uname = empty($uOne) ? '-' : $uOne['uname'];
            $yiyuan = (array) $this->dbConn($this->dbYiyuan)
                ->where('id', $v['yiyuan'])
                ->first();
            $yiyuan_name = empty($yiyuan) ? '-' : $yiyuan['title'];
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'uname' => $uname,
                'yytime' => $v['yytime'],
                'yiyuan_name' => $yiyuan_name,
                'remark' => $v['remark'],
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //代办详情
    public function daibanOne($id)
    {
        $res = (array) $this->dbConn($this->dbDaiban)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $res['uid'])
            ->first();
        $uname = empty($uOne) ? '-' : $uOne['uname'];
        $yiyuan = (array) $this->dbConn($this->dbYiyuan)
            ->where('id', $res['yiyuan'])
            ->first();
        $yiyuan_name = empty($yiyuan) ? '-' : $yiyuan['title'];
        $title='其他代办';
        if($res['type']){
            $departOne = (array) $this->dbConn($this->dbCate)
                ->where('del', 0)
                ->where('id', $res['type'])
                ->first();
            if($departOne){
                $title=$departOne['title'].'代办';
            }
        }

        $arys = [
            'id' => $res['id'],
            'uid' => $res['uid'],
            'uname' => $uname,
            'title' => $title,
            'yytime' => $res['yytime'],
            'yiyuan_name' => $yiyuan_name,
            'remark' => $res['remark'],
            'addtime' => $res['addtime'],
        ];
        return $this->toback(0, '', $arys);
    }
}